$(function(){
    //设置导航高亮
    navActive();

    layui.use(['laydate','table','util'],function(){
        var laydate = layui.laydate,
            table = layui.table,
            util = layui.util;

        //执行一个laydate实例
        laydate.render({
            elem: '#startDate'
            ,value: formatToday() //默认当前日期
        });
        laydate.render({
            elem: '#endDate'
            ,value: formatToday()
        });

        //不传参，默认查全部
        getOriginalData();

        //查询按钮
        $(".searchBtn").click(function(){
            var dataArr = $("#params").serializeArray();
            var dataObj = {};
            $(dataArr).each(function(i,v){
                dataObj[v.name] = v.value;
            });
            // console.log(dataObj);
            getOriginalData(dataObj);
            return false; //阻止表单默认刷新跳转
        });

        //获取数据
        function getOriginalData(dataObj){
            $.ajax({
                type: "GET",
                url: ctx+"/originalReport/findAll.do",
                data: dataObj || {},  //不传参默认空对象
                dataType: "json",
                success: function(data){

                    var colsArr = [ //展示的列
                        {field:'meterCode', title: '芯片编号'},
                        {field:'currentQuantiry', title: '水表读数'},
                        {field:'electricQty', title: '电量'},
                        {field:'receiveQuantiry', title: '水表原始读数'},
                        {field:'uploadDate', title: '所属日期'},
                        {field:'addedTime', title: '上传时间',templet:'<div>{{layui.util.toDateString(d.addedTime)}}</div>'}
                    ];
                    originalReport(data,colsArr);
                }
            });
        }

        //导出模板;导出EXCEL
        $("body").on("click",".exportBtn",function(){
            var form = $("<form></form>");
            form.appendTo("body");
            form.attr("action", ctx+"/originalReport/export.do");
            form.attr("method", "post");
            form.submit();
            return false;
        });

        //渲染表格
        function originalReport(data,colsArr){
            table.render({
                elem: '#original-list',
                data: data,
                title: '原始报表',
                cols: [colsArr],
                page: {
                    layout: ['prev', 'page', 'next', 'count','skip','limit']
                    ,groups: 5 //只显示 5 个连续页码
                    ,first: '首页' //显示首页
                    ,last: '尾页' //显示尾页
                    ,limit: 15  //每页显示条数
                    ,limits : [15,30,45,60]
                },
                loading: true
            });
        }
    });
});


